home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / ktrans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  5.0 KB  |  173 lines

  1. /*++
  2. /* NAME
  3. /*    ktrans 3
  4. /* SUMMARY
  5. /*    k-protocol strategy routines
  6. /* PACKAGE
  7. /*    uucp across the TUEnet
  8. /* SYNOPSIS
  9. /*    #include "kp.h"
  10. /*
  11. /*    krproto(fd,data,size) 
  12. /*    int fd, *size;
  13. /*    char data[MAXPACKSIZ];
  14. /*
  15. /*    kwproto(fd,data,size) 
  16. /*    int fd, size;
  17. /*    char data[MAXPACKSIZ];
  18. /*
  19. /*    kclsproto(fd)
  20. /*    int fd;
  21. /* DESCRIPTION
  22. /*    The functions in this file take care of handshake and error
  23. /*    detection/recovery. The read/write functions return through an
  24. /*    external function kfail() in case of fatal errors, or protocol 
  25. /*    termination by the network partner.
  26. /*
  27. /*    The following packet types are used:
  28. /*
  29. /* .nf
  30. /* .in +5
  31. /*    D packets contain data.
  32. /*    Y packets are sent when a correct data packet was received.
  33. /*    N packets are sent when incorrect data was received.
  34. /*    A packets are sent to shut down the k protocol.
  35. /* .fi
  36. /*
  37. /*    Krproto() sends the data and either returns normally, or through 
  38. /*    kfail().
  39. /*
  40. /*    Kwproto() returns the received data or returns through kfail().
  41. /*
  42. /*    Kclsproto() sends the protocol termination sequence to the other 
  43. /*    side, either to signal the end of transfers, or to confirm
  44. /*    reception of an end-of-protocol sequence.
  45. /*
  46. /*    The strategy for sending data is as follows:
  47. /*
  48. /*    Send packet.
  49. /*    If (ACK for last packet) or (NAK for next packet) received, terminate.
  50. /*    If (NAK for last packet) or no response received, retransmit.
  51. /*    If data received with previous packet nr, ignore and wait for NAK.
  52. /*    If data received with next packet nr, NAK that data and terminate.
  53. /*    Otherwise (bad packet number, unexpected packet type) abort.
  54. /*
  55. /*    The strategy for receiving data is complementary:
  56. /*
  57. /*    Wait for packet
  58. /*    If expected packet received, ACK it and terminate.
  59. /*    If previous data packet received, ACK it and wait for next packet.
  60. /*    If bad packet received, send NAK for expected packet.
  61. /*    If nothing received, wait for another packet.
  62. /*    Otherwise (bad packet number, unexpected packet type) abort.
  63. /* FUNCTIONS AND MACROS
  64. /*    kspack, krpack, kfail
  65. /* AUTHOR(S)
  66. /*    Wietse Venema
  67. /*    Eindhoven University of Technology
  68. /*    Department of Mathematics and Computer Science
  69. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  70. /* CREATION DATE
  71. /*    Mon Feb  3 13:12:08 MET 1986
  72. /* LAST MODIFICATION
  73. /*    90/01/22 13:01:59
  74. /* VERSION/RELEASE
  75. /*    2.1
  76. /*--*/
  77.  
  78. #include "kp.h"
  79.  
  80. static char recpkt[MAXPACKSIZ];            /* receive packet buffer */
  81. static int  n = 0;                /* packet number */
  82.  
  83. kwproto( fd, packet, size)
  84. int fd;
  85. char *packet;
  86. int size;
  87. {
  88.     int num, numtry;                            /* Packet number, tries */
  89.     int len;
  90.  
  91.     kspack(fd, 'D',n,size,packet);          /* Send a D packet */
  92.  
  93.     for (numtry = 0; numtry < MAXTRY; numtry++) {
  94.     switch(krpack(fd,&num,&len,recpkt))     /* What was the reply? */
  95.     {
  96.     case 'D':                    /* DATA */
  97.         if ((num+1)%64 == n) {        /* Previous packet ? */
  98.         numtry = 0;            /* Reset counter */
  99.         break;                /* Don't ack it; read again */
  100.         } else if (num != (n+1)%64)        /* Fatal error, unless it */
  101.         kfail();                /* carries next packet number */
  102.         kspack(fd,'N',num,0,NULLP);       /* Can't use data now */
  103.  
  104.     case 'N':                           /* NAK */
  105.         if (n == num) {            /* Send another one, */
  106.         kspack(fd, 'D',n,size,packet);
  107.         break;
  108.         }
  109.         num = (--num<0 ? 63:num);     /* unless NAK for next packet */
  110.  
  111.     case 'Y':                           /* ACK */
  112.         if (n != num) kfail();          /* If wrong ACK, fail */
  113.         n = (n+1)%64;                   /* Bump packet count */
  114.         return;
  115.  
  116.     case TIME:                /* No response */
  117.     case FAIL:                /* Bad packet */
  118.         kspack(fd, 'D',n,size,packet);
  119.         break;                  /* Send data packet again */
  120.  
  121.     default: 
  122.         kfail();                           /* Something else, abort */
  123.     }
  124.     }
  125.     kfail();                        /* Too may retries, abort */
  126. }
  127.  
  128. krproto( fd, packet, size)
  129. int fd;
  130. char *packet;
  131. int *size;
  132. {
  133.     int num, numtry;                            /* Packet number, tries */
  134.  
  135.     for (numtry = 0; numtry < MAXTRY; numtry++) 
  136.     {
  137.     switch (krpack(fd,&num,size,packet))    /* Get packet */
  138.     {
  139.     case 'D':
  140.         if (num == n) {            /* Right packet? */
  141.         kspack(fd,'Y',n,0,NULLP);    /* Acknowledge the packet */
  142.         n = (n+1)%64;            /* Bump packet number, mod 64 */
  143.         return;
  144.         } else if (num == ((n==0) ? 63:n-1)) { /* Previous packet? */
  145.         kspack(fd,'Y',num,0,NULLP);    /* Re-ack previous packet */
  146.         numtry = 0;                    /* Reset counter */
  147.         break;                /* Read another packet */
  148.         } else
  149.         kfail();            /* Sorry, wrong number */
  150.  
  151.     case TIME:                /* No packet */
  152.         break;                /* Don't NAK */
  153.  
  154.     case FAIL:                /* Bad packet */
  155.         kspack(fd,'N',n,0,NULLP);        /* Return a NAK */
  156.         break;                /* Read another packet */
  157.  
  158.     default:
  159.         kfail();                /* Something else, abort */
  160.     }
  161.     }
  162.     kfail();                        /* Too may retries, abort */
  163. }
  164.  
  165. kclsproto( fd)
  166. int fd;
  167. {
  168.     kspack( fd, 'A', 0, 0, NULLP);        /* Send an 'A' packet */
  169.     kspack( fd, 'A', 0, 0, NULLP);        /* and another two since */
  170.     kspack( fd, 'A', 0, 0, NULLP);        /* we won't get ACKs */
  171.     return 0;
  172. }
  173.